package com.hmsg.health.utils;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 
 * @author lujq
 *
 */
public class RandomUtil {
	private RandomUtil() {
	}

	public static String genRandomNum(int n) {
		Random random = new Random();
		String sRand = "";
		for (int i = 0; i < n; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		if (sRand.substring(0, 1).equals("0")) {
			sRand = "1" + sRand.substring(1);
		}
		return sRand;
	}

	public static <T> T random(List<T> list) {
		return list.get(new Random().nextInt(list.size()));
	}

	public static <T> T random(T[] arr) {
		if (arr == null) {
			return null;
		}
		return arr[new Random().nextInt(arr.length)];
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("\\-", "").toUpperCase();
	}

	public static String getRandomString(int length) { // length表示生成字符串的长度
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
}
